//
//  ESCheckVersion.m
//  成都餐饮
//
//  Created by tony on 14-5-5.
//  Copyright (c) 2014年 wang yongfeng. All rights reserved.
//

#import "ESCheckVersion.h"

#define APP_ID @"860454840"//成都团购
@implementation ESCheckVersion

- (void)checkAppVersionSynchronous
{
    NSError *error = nil;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APP_ID]];
    NSURLRequest *requset = [[NSURLRequest alloc] initWithURL:url];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:requset returningResponse:nil error:&error];
    if(returnData == nil && error != nil)
    {
        [self.delegate checkVersionFailed:self];
        return;
    }
    NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
    NSArray *dataArray = [jsonData objectForKey:@"results"];
    NSDictionary *mydic = [dataArray objectAtIndex:0];
    self.trackViewUrl = [mydic objectForKey:@"trackViewUrl"];
    self.trackName = [mydic objectForKey:@"trackName"];
    NSString *latestVersion = [mydic objectForKey:@"version"];
    NSDictionary *inforDict = [[NSBundle mainBundle] infoDictionary];
    NSString *curretnVersion = [inforDict objectForKey:@"CFBundleVersion"];
    double doubleCurrentVersion = [curretnVersion doubleValue];
    double doubleLatestVersion = [latestVersion doubleValue];
    if(doubleCurrentVersion < doubleLatestVersion)
    {
        //有新版本
        self.bVersion = YES;
        [self.delegate checkVersion:self State:YES];
    }else
    {
        //没有更新
        self.bVersion = NO;
        [self.delegate checkVersion:self State:NO];
    }
}

- (void)checkAppVersionAsynchronous
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APP_ID]];
    NSURLRequest *requset = [[NSURLRequest alloc] initWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if(data == nil || connectionError != nil)
        {
            [self.delegate checkVersionFailed:self];
            return;
        }
        NSData *returnData = data;
        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
        NSArray *dataArray = [jsonData objectForKey:@"results"];
        NSDictionary *mydic = [dataArray objectAtIndex:0];
        self.trackViewUrl = [mydic objectForKey:@"trackViewUrl"];
        self.trackName = [mydic objectForKey:@"trackName"];
        NSString *latestVersion = [mydic objectForKey:@"version"];
        NSDictionary *inforDict = [[NSBundle mainBundle] infoDictionary];
        NSString *curretnVersion = [inforDict objectForKey:@"CFBundleVersion"];
        double doubleCurrentVersion = [curretnVersion doubleValue];
        double doubleLatestVersion = [latestVersion doubleValue];
        if(doubleCurrentVersion < doubleLatestVersion)
        {
            //有新版本
            self.bVersion = YES;
            [self.delegate checkVersion:self State:YES];
        }else
        {
            //没有更新
            self.bVersion = NO;
            [self.delegate checkVersion:self State:NO];
        }
    }];
}

@end
